<!DOCTYPE html>
<html>
<head>
		    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>

<h1 id="title">正则测试</h1>

<script type="text/javascript">

	var str="The best free things in life are free";
	a();
	b();
	function a()
	{
		var reg=/free/g;//g  =>   global search .

		var reg=new RegExp("free","g");//g  =>   global search .
		console.log(execAll(reg,str));
	}
	function b()
	{
		var reg=new RegExp("free","g");//g  =>   global search .
		console.log(execAll(reg,str));
	}

	function execAllReg(reg,str)
	{
		var result=new Array();
		var rs
		while((rs=reg.exec(str))!=null)  //获取所有匹配的字符串
		{
			result.push(rs);
		}
		return result;
	}

</script>
</body>
</html>

